<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 4.2.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/all.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"chentianming11.github.io","root":"/","scheme":"Gemini","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":true,"show_result":true,"style":"default"},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":{"enable":true,"onlypost":false,"loadingImg":"/uploads/loading.gif"},"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.xml"};
  </script>

  <meta name="description" content="在实际生产环境中，为了避免单点故障，Redis肯定不会以单机的形式部署，而是会同时部署多个实例。Redis多机数据库的实现主要有三种方式，分别是主从复制、Sentinel和集群，本文主要会详细阐述主从复制的实现原理。">
<meta property="og:type" content="article">
<meta property="og:title" content="Redis分布式-主从复制详解">
<meta property="og:url" content="https://chentianming11.github.io/posts/3441087838/index.html">
<meta property="og:site_name" content="伍六七">
<meta property="og:description" content="在实际生产环境中，为了避免单点故障，Redis肯定不会以单机的形式部署，而是会同时部署多个实例。Redis多机数据库的实现主要有三种方式，分别是主从复制、Sentinel和集群，本文主要会详细阐述主从复制的实现原理。">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://chentianming11.github.io/images/redis/sync.png">
<meta property="og:image" content="https://chentianming11.github.io/images/redis/psync.png">
<meta property="og:image" content="https://chentianming11.github.io/images/redis/psyncshixian.png">
<meta property="article:published_time" content="2020-11-01T06:29:54.000Z">
<meta property="article:modified_time" content="2020-11-01T09:18:45.722Z">
<meta property="article:author" content="陈添明">
<meta property="article:tag" content="redis">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://chentianming11.github.io/images/redis/sync.png">

<link rel="canonical" href="https://chentianming11.github.io/posts/3441087838/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>Redis分布式-主从复制详解 | 伍六七</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">伍六七</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
      <p class="site-subtitle" itemprop="description">一起学Java</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签<span class="badge">6</span></a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类<span class="badge">3</span></a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档<span class="badge">17</span></a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result">
  <div id="no-result">
    <i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>
  </div>
</div>

    </div>
  </div>

</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>

  <a href="https://github.com/chentianming11" class="github-corner" title="Follow me on GitHub" aria-label="Follow me on GitHub" rel="noopener" target="_blank"><svg width="80" height="80" viewBox="0 0 250 250" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></a>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://chentianming11.github.io/posts/3441087838/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.jpg">
      <meta itemprop="name" content="陈添明">
      <meta itemprop="description" content="一个爱看国漫的程序猿">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="伍六七">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          Redis分布式-主从复制详解
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>
              

              <time title="创建时间：2020-11-01 14:29:54 / 修改时间：17:18:45" itemprop="dateCreated datePublished" datetime="2020-11-01T14:29:54+08:00">2020-11-01</time>
            </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/%E6%95%B0%E6%8D%AE%E5%BA%93/" itemprop="url" rel="index"><span itemprop="name">数据库</span></a>
                </span>
            </span>

          
  
  <span class="post-meta-item">
    
      <span class="post-meta-item-icon">
        <i class="far fa-comment"></i>
      </span>
      <span class="post-meta-item-text">Valine：</span>
    
    <a title="valine" href="/posts/3441087838/#valine-comments" itemprop="discussionUrl">
      <span class="post-comments-count valine-comment-count" data-xid="/posts/3441087838/" itemprop="commentCount"></span>
    </a>
  </span>
  
  <br>
            <span class="post-meta-item" title="本文字数">
              <span class="post-meta-item-icon">
                <i class="far fa-file-word"></i>
              </span>
                <span class="post-meta-item-text">本文字数：</span>
              <span>2.5k</span>
            </span>
            <span class="post-meta-item" title="阅读时长">
              <span class="post-meta-item-icon">
                <i class="far fa-clock"></i>
              </span>
                <span class="post-meta-item-text">阅读时长 &asymp;</span>
              <span>6 分钟</span>
            </span>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <p>在实际生产环境中，为了避免单点故障，<code>Redis</code>肯定不会以单机的形式部署，而是会同时部署多个实例。<strong><code>Redis</code>多机数据库的实现主要有三种方式，分别是主从复制、Sentinel和集群，本文主要会详细阐述主从复制的实现原理</strong>。</p>
<a id="more"></a>

<blockquote>
<p>本文主要内容参考自《Redis设计与实现》</p>
</blockquote>
<h2 id="如何使用主从复制"><a href="#如何使用主从复制" class="headerlink" title="如何使用主从复制"></a>如何使用主从复制</h2><p>在<code>Redis</code>中，可以通过<code>SLAVEOF</code>命令或者设置<code>slaveof</code>选项，可以让一个服务器去复制另一个服务器。被复制的服务器称为主服务器(master)，进行复制的服务器称为从服务器。</p>
<h2 id="旧版复制功能实现"><a href="#旧版复制功能实现" class="headerlink" title="旧版复制功能实现"></a>旧版复制功能实现</h2><p>在2.8版本之前，<code>Redis</code>的复制主要分为<strong>同步(sync)</strong>和<strong>命令传播(command propagate)</strong>两个操作：</p>
<ol>
<li>同步：将从服务器的数据库状态更新成主服务器的数据库状态。</li>
<li>命令传播：将作用于主服务器的写命令，传播给从服务器进行执行，从而保证主从数据库状态一致。</li>
</ol>
<h3 id="同步"><a href="#同步" class="headerlink" title="同步"></a>同步</h3><p>当从服务器刚执行<code>SLAVEOF</code>命令时，首先做的就是同步操作，将从服务器的数据库状态更新成主服务器的数据库状态。</p>
<p>从服务器通过向主服务器发送<code>SYNC</code>命令来完成同步操作，<code>SYNC</code>命令的执行步骤如下：</p>
<ol>
<li>从服务器向主服务器发送<code>SYNC</code>命令。</li>
<li>主服务器收到<code>SYNC</code>命令之后，开始执行<code>BGSAVE</code>命令生成<code>RDB</code>文件。在生成<code>RDB</code>文件期间，将写命令记录在一个缓冲区中。</li>
<li>主服务器发送<code>RDB</code>文件文件给从服务器，从服务器载入该<code>RDB</code>文件。</li>
<li>主服务器发送生成<code>RDB</code>文件期间的写命令给从服务器，从服务器重放这些命令。此时从服务器状态和主服务器状态一致，同步操作完成。</li>
</ol>
<p><img data-src="https://chentianming11.github.io/images/redis/sync.png" alt="sync"></p>
<h3 id="命令传播"><a href="#命令传播" class="headerlink" title="命令传播"></a>命令传播</h3><p>在同步操作完成之后，后续主服务器执行的写命令会以命令传播的方式发送给从服务器，从而保证主从数据库状态一致。</p>
<h3 id="旧版复制功能的缺陷"><a href="#旧版复制功能的缺陷" class="headerlink" title="旧版复制功能的缺陷"></a>旧版复制功能的缺陷</h3><p>当初次复制时，旧版复制功能没有任何问题。但是当因为网络问题，主从服务器短暂掉线重连之后，此时依然会触发复制。<strong>虽然这种方式可以让主从数据库状态重新一致，但是性能和效率都会很低</strong>。因为重连之后的复制，仍然要执行同步操作，生成完整的<code>RDB</code>文件，从服务器依然要载入该<code>RDB</code>文件。实际上，在掉线期间，只是丢失了少量写命令，此时执行完整的复制操作，显然效率太低了。</p>
<h2 id="新版复制功能实现"><a href="#新版复制功能实现" class="headerlink" title="新版复制功能实现"></a>新版复制功能实现</h2><p>为了解决旧版复制功能在掉线重复制的低效问题，在2.8版本之后，<code>Redis</code>使用<code>PSYNC</code>命令代替原来的<code>SYNC</code>命令来执行同步操作。</p>
<p><strong><code>PSYNC</code>命令具有完整重同步(full resynchronization)和部分重同步(patial resynchronization)两种模式</strong>：</p>
<ul>
<li>完整重同步：用于处理初次复制的情况。跟旧版的<code>SYNC</code>命令基本一致，都是让主服务器生成并发送<code>RDB</code>文件和在此期间的写命令给从服务器来完成同步操作的。</li>
<li>部分重同步：用于处理掉线后的重复制情况。当从服务器掉线后重新连接主服务器，如果条件允许，可以让主服务器只将掉线期间执行的写命令发送给从服务器，从而达到主从数据库状态重新一致。</li>
</ul>
<p><img data-src="https://chentianming11.github.io/images/redis/psync.png" alt="psync"></p>
<h2 id="部分重同步实现"><a href="#部分重同步实现" class="headerlink" title="部分重同步实现"></a>部分重同步实现</h2><p>部分重同步功能主要由以下三个部分构成：</p>
<ul>
<li>主服务器的复制偏移量(replication offset)和从服务器的复制偏移量</li>
<li>主服务器的复制积压缓冲区(replication backlog)</li>
<li>服务器运行ID</li>
</ul>
<h3 id="复制偏移量"><a href="#复制偏移量" class="headerlink" title="复制偏移量"></a>复制偏移量</h3><p>执行复制的双方-主服务器和从服务器会分别维护一个复制偏移量：</p>
<ul>
<li>主服务器每次向从服务器传播N个字节的数据时，就会将自己的复制偏移量加N。</li>
<li>从服务器每次收到主服务器传播来的N个字节数据时，也会将自己的复制偏移量加N。</li>
</ul>
<p><strong>如果主从数据库状态一致，那么它们的复制偏移量一定是相等的，反之，则表示数据库状态不一致</strong>。因此当发生掉线重连时，主从服务器的复制偏移量一定不相等。</p>
<h3 id="复制积压缓存区"><a href="#复制积压缓存区" class="headerlink" title="复制积压缓存区"></a>复制积压缓存区</h3><p><strong>复制积压缓存区是由主服务器维护的固定长度的先进先出(FIFO)队列，默认大小为<code>1MB</code>。当主服务器进行命令传播时，不仅会将写命令发送给所有的从服务器，还会将写命令写入复制积压缓冲区中</strong>。因此，复制积压缓冲区会保存最近传播的写命令，并且复制积压缓冲区为每个字节记录了相应的复制偏移量。</p>
<p>当从服务器重连之后，从服务器会通过<code>PSYNC</code>命令将自己的复制偏移量发送给主服务器，主服务器会根据这个偏移量来决定采用哪种同步操作。</p>
<ul>
<li>如果<code>offset</code>偏移量之后的数据仍然存在复制积压缓冲区中，那么采用部分重同步。</li>
<li>如果<code>offset</code>偏移量之后的数据不在复制积压缓冲区中，那么采用完整重同步。</li>
</ul>
<h3 id="服务器运行ID"><a href="#服务器运行ID" class="headerlink" title="服务器运行ID"></a>服务器运行ID</h3><p><strong>每个<code>Redis</code>服务器，不论是主服务器还是从服务器，都会有自己的运行ID。运行ID在服务器启动的时候生成，由40位随机字符组成。当从服务器进行初次复制时，会将对应的主服务器运行ID也保存下来</strong>。通过这个运行ID，就能确定重连上的主服务器是不是掉线之前的主服务器，如果是的话，则尝试之前的部分重同步，反之，则执行完整重同步。</p>
<h3 id="PSYNC命令的实现"><a href="#PSYNC命令的实现" class="headerlink" title="PSYNC命令的实现"></a>PSYNC命令的实现</h3><p>PSYNC命令调用方式有两种：</p>
<ol>
<li>如果从服务器之前没有复制过任何主服务器，那么从服务器在开始一次新的复制时，会发送<code>PSYNC ? -1</code>命令，主动请求完整重同步。</li>
<li>如果从服务器已经复制过主服务器，那么开始一次新的复制时，会发送<code>PSYNC &lt;runid&gt; &lt;offset&gt;</code>命令，主服务根据<code>runid</code>和<code>offset</code>参数决定使用何时复制方式。</li>
</ol>
<p>接收到<code>PSYNC</code>命令的主服务器，可能会返回以下三种结果中的一种给从服务器：</p>
<ol>
<li><code>+FULLRESYNC &lt;runid&gt; &lt;offset&gt;</code>：执行完整重同步。其中<code>runid</code>表示主服务器的运行ID，从服务器会将其保存下来；<code>offset</code>是主服务器的复制偏移量，从服务器会将其作为自己的初始偏移量。</li>
<li><code>+CONTINUE</code>：执行部分重同步。主服务器会将缺少的那部分数据发送给从服务器。</li>
<li><code>-ERR</code>：复制错误。表示主服务器版本低于2.8，不能识别<code>PSYNC</code>命令。</li>
</ol>
<p><img data-src="https://chentianming11.github.io/images/redis/psyncshixian.png" alt="psyncshixian"></p>
<h2 id="主从复制的不足"><a href="#主从复制的不足" class="headerlink" title="主从复制的不足"></a>主从复制的不足</h2><p>主从复制解决了数据备份和性能(通过读写分离)的问题，但是还是存在一些不足:</p>
<ol>
<li>RDB 文件过大的情况下，同步非常耗时。</li>
<li>在一主一从或者一主多从的情况下，如果主服务器挂了，对外提供的服务就不可用了，单点问题没有得到解决。如果每次都是手动把之前的从服务器切换成主服务器，这个比较费时费力，还会造成一定时间的服务不可用。</li>
</ol>
<blockquote>
<p>原创不易，觉得文章写得不错的小伙伴，点个赞👍 鼓励一下吧~</p>
</blockquote>

    </div>

    
    
    

      <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/redis/" rel="tag"># redis</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/posts/4169530895/" rel="prev" title="深入理解Redis持久化机制-RDB、AOF实现详解">
      <i class="fa fa-chevron-left"></i> 深入理解Redis持久化机制-RDB、AOF实现详解
    </a></div>
      <div class="post-nav-item">
    <a href="/posts/2663121991/" rel="next" title="Redis分布式-集群详解">
      Redis分布式-集群详解 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          
    <div class="comments" id="valine-comments"></div>

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#如何使用主从复制"><span class="nav-number">1.</span> <span class="nav-text">如何使用主从复制</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#旧版复制功能实现"><span class="nav-number">2.</span> <span class="nav-text">旧版复制功能实现</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#同步"><span class="nav-number">2.1.</span> <span class="nav-text">同步</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#命令传播"><span class="nav-number">2.2.</span> <span class="nav-text">命令传播</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#旧版复制功能的缺陷"><span class="nav-number">2.3.</span> <span class="nav-text">旧版复制功能的缺陷</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#新版复制功能实现"><span class="nav-number">3.</span> <span class="nav-text">新版复制功能实现</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#部分重同步实现"><span class="nav-number">4.</span> <span class="nav-text">部分重同步实现</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#复制偏移量"><span class="nav-number">4.1.</span> <span class="nav-text">复制偏移量</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#复制积压缓存区"><span class="nav-number">4.2.</span> <span class="nav-text">复制积压缓存区</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#服务器运行ID"><span class="nav-number">4.3.</span> <span class="nav-text">服务器运行ID</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#PSYNC命令的实现"><span class="nav-number">4.4.</span> <span class="nav-text">PSYNC命令的实现</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#主从复制的不足"><span class="nav-number">5.</span> <span class="nav-text">主从复制的不足</span></a></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="陈添明"
      src="/images/avatar.jpg">
  <p class="site-author-name" itemprop="name">陈添明</p>
  <div class="site-description" itemprop="description">一个爱看国漫的程序猿</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">17</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">3</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">6</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2020</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">陈添明</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-chart-area"></i>
    </span>
    <span title="站点总字数">156k</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
    <span title="站点阅读时长">6:30</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Gemini</a> 强力驱动
  </div>

        








      </div>
    </footer>
  </div>

  
  <script size="300" alpha="0.6" zIndex="-1" src="//cdn.jsdelivr.net/gh/theme-next/theme-next-canvas-ribbon@1/canvas-ribbon.js"></script>
  <script src="/lib/anime.min.js"></script>
  <script src="//cdn.jsdelivr.net/gh/theme-next/theme-next-pjax@0/pjax.min.js"></script>
  <script src="//cdn.jsdelivr.net/npm/lozad@1/dist/lozad.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/pisces.js"></script>


<script src="/js/next-boot.js"></script>

  <script>
var pjax = new Pjax({
  selectors: [
    'head title',
    '#page-configurations',
    '.content-wrap',
    '.post-toc-wrap',
    '.languages',
    '#pjax'
  ],
  switches: {
    '.post-toc-wrap': Pjax.switches.innerHTML
  },
  analytics: false,
  cacheBust: false,
  scrollTo : !CONFIG.bookmark.enable
});

window.addEventListener('pjax:success', () => {
  document.querySelectorAll('script[data-pjax], script#page-configurations, #pjax script').forEach(element => {
    var code = element.text || element.textContent || element.innerHTML || '';
    var parent = element.parentNode;
    parent.removeChild(element);
    var script = document.createElement('script');
    if (element.id) {
      script.id = element.id;
    }
    if (element.className) {
      script.className = element.className;
    }
    if (element.type) {
      script.type = element.type;
    }
    if (element.src) {
      script.src = element.src;
      // Force synchronous loading of peripheral JS.
      script.async = false;
    }
    if (element.dataset.pjax !== undefined) {
      script.dataset.pjax = '';
    }
    if (code !== '') {
      script.appendChild(document.createTextNode(code));
    }
    parent.appendChild(script);
  });
  NexT.boot.refresh();
  // Define Motion Sequence & Bootstrap Motion.
  if (CONFIG.motion.enable) {
    NexT.motion.integrator
      .init()
      .add(NexT.motion.middleWares.subMenu)
      .add(NexT.motion.middleWares.postList)
      .bootstrap();
  }
  NexT.utils.updateSidebarPosition();
});
</script>




  




  
<script src="/js/local-search.js"></script>













    <div id="pjax">
  

  
  <script src="//cdn.jsdelivr.net/npm/quicklink@1/dist/quicklink.umd.js"></script>
  <script>
      window.addEventListener('load', () => {
      quicklink({
        timeout : 3000,
        priority: true,
        ignores : [uri => uri.includes('#'),uri => uri === 'https://chentianming11.github.io/posts/3441087838/',]
      });
      });
  </script>


<script>
NexT.utils.loadComments(document.querySelector('#valine-comments'), () => {
  NexT.utils.getScript('//unpkg.com/valine/dist/Valine.min.js', () => {
    var GUEST = ['nick', 'mail', 'link'];
    var guest = 'nick,mail';
    guest = guest.split(',').filter(item => {
      return GUEST.includes(item);
    });
    new Valine({
      el         : '#valine-comments',
      verify     : false,
      notify     : true,
      appId      : 'SoPxUkme2aynTULaNuIc5TIq-gzGzoHsz',
      appKey     : '4TDP7SMCbgDKTOGGUtpIeA0H',
      placeholder: "留下点足迹吧~",
      avatar     : 'mm',
      meta       : guest,
      pageSize   : '10' || 10,
      visitor    : false,
      lang       : '' || 'zh-cn',
      path       : location.pathname,
      recordIP   : false,
      serverURLs : ''
    });
  }, window.Valine);
});
</script>

    </div>
</body>
</html>
